//
//  EGAUtility.m
//  EGA_Archives_iOS
//
//  Created by aekcachai chokeprajugchut on 4/18/56 BE.
//  Copyright (c) 2556 EGA. All rights reserved.
//

#import "EGAUtility.h"

@implementation EGAUtility


-(NSData*)getDataFromService:(NSString*)postData
{
    NSString * uid =@"EGA_Tracking";
    NSString * Consumer_Key =@"19969eba-c6ac-4830-8cbc-d0532175dfb4";                      //generate by EGA
    NSString * consumerSecret=@"5OjzP0BD76t";                             //generate by EGA
    NSString *completeString = @"https://egaws.ega.or.th/ws/auth/validate";
    NSURL *urlForValidation = [NSURL URLWithString:completeString];
    NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
    [validationRequest setHTTPMethod:@"POST"];
    [validationRequest setValue:Consumer_Key forHTTPHeaderField:@"Consumer-Key"];
    NSString *strTest = strTest=[NSString stringWithFormat:@"consumerSecret=%@&uid=%@",consumerSecret,uid ];
    [validationRequest setHTTPBody:[strTest dataUsingEncoding:NSUTF8StringEncoding]];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];
    //NSString *response = [[NSString alloc] initWithData: responseData encoding: NSUTF8StringEncoding];
    //NSLog(@"%@", response);
    NSError *error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSString *TokenKey=[json objectForKey:@"Result"];
    NSString * servicePath = @"https://egaws.ega.or.th/ws/ega/archive/access";
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL: [NSURL URLWithString: servicePath]];
	NSString *post = postData;
    NSData *postData_ = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData_ length]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:Consumer_Key forHTTPHeaderField:@"Consumer-Key"];
    [request setValue:TokenKey forHTTPHeaderField:@"Token"];
    [request setHTTPBody:postData_];
	//Data returned by WebService
    NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse: nil error: nil ];
    //NSDictionary *jsonx = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
    return returnData;
}

-(NSString*)getDateTh:(NSString*)strDate
{
    
    NSString *str = strDate;
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString: str];
    dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
//    [dateFormatter setDateFormat:@"dd/MM/yyyy"];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *convertedString = [dateFormatter stringFromDate:date];
    NSDate *now = date;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy"];
    NSString *year = [formatter stringFromDate:now];
    [formatter setDateFormat:@"MM"];
    NSString *month = [formatter stringFromDate:now];
    [formatter setDateFormat:@"dd"];
    NSString *day = [formatter stringFromDate:now];
    NSString *yearString = year;
    int yearInt = [yearString intValue];
    int dayInt = [day intValue];
    if (dayInt < 10) {day=[NSString stringWithFormat:@"%d",dayInt];  }
    if (yearInt<2500)
    {
        yearInt=yearInt+543;
        year=[NSString stringWithFormat:@"%d",yearInt];
    }
    
    NSString *monthString=@"";
    if ([month isEqualToString:@"01"]) {
        monthString = @"ม.ค.";
    }
    else  if ([month isEqualToString:@"02"]) {
        monthString = @"ก.พ.";
    }
    else  if ([month isEqualToString:@"03"]) {
        monthString = @"มี.ค.";
    }
    else  if ([month isEqualToString:@"04"]) {
        monthString = @"เม.ย.";
    }
    else  if ([month isEqualToString:@"05"]) {
        monthString = @"พ.ค.";
    }
    else  if ([month isEqualToString:@"06"]) {
        monthString = @"มิ.ย.";
    }
    else  if ([month isEqualToString:@"07"]) {
        monthString = @"ก.ค.";
    }
    else  if ([month isEqualToString:@"08"]) {
        monthString = @"ส.ค.";
    }
    else  if ([month isEqualToString:@"09"]) {
        monthString = @"ก.ย.";
    }
    else  if ([month isEqualToString:@"10"]) {
        monthString = @"ต.ค.";
    }
    else  if ([month isEqualToString:@"11"]) {
        monthString = @"พ.ย.";
    }
    else  if ([month isEqualToString:@"12"]) {
        monthString = @"ธ.ค.";
    }
    convertedString=[NSString stringWithFormat:@"%1@ %1@ %3@ ",day,monthString,year];
    return convertedString;
}
@end
